#Version 1.0.3

package MerchChat;

use strict;
use warnings;
use Plugins;
use Log qw(message);

use Globals;
use Network;
use Network::Receive;
use Network::Send ();

my @title = (
	"LOOKING For ITEM? PM me",
	"ONLINE ITEM Searcher PM me",
	"NO Buyer? PM me",
	"Check mo Items namin PM me",
	"Pagod ka magHanap? PM me ^^",
	"I-google ang ITEM! PM me",
	"NEW TECHNIQUE sa ITEM PM me",
	"Madami akong +9! PM me",
	"Different Buyer/Cost PM na"
	);
my @menu = (
	"==Merchant's List ONLINE========v1.0.3",
	"1. Anong meron? (Introduction)",
	"2. Anong mapapala ko? (Benifit)",
	"3. May bayad ba? (FEE)",
	"4. AYAW KO NG SCAM! (Scam Issue)",
	"5. Anong ang website? (Service)",
	"Choose a number [1-5]"
	);
my @ans1 = (
	"Ito ay isang SERVICE para sa mga manlalaro",
	"ng RAGNAROK."
	);
my @ans2 = (
	"Malake ang benipisyo nyo bilang manlalaro",
	"ng RAGNAROK dahil dito po kayo:",
	" - Maghahanap ng ITEM at ibibigay sa inyo kung",
	"   kanino mabibili, magkano at san makikita.",
	" - MagBlog ng ITEM na ibebenta nyo at icocontact",
	"   kayo ng buyer."
	);
my @ans3 = (
	"Ito po ay LIBRE, kahit magBLOG o magSearch",
	"kayo ng ITEM hindi po kami sumisingil.",
	"Pero kung magDodonate kayo, tatanggap kami ^^"
	);
my @ans4 = (
	"Wala po akong hihingiin na pera sa inyo",
	"Kayo at ang buyer/dealer nyo ang mag-tratransact",
	"at sa GAME kayo mag-uusap."
	);
my @ans5 = (
	"==================================",
	"Merchant's List ONLINE!",
	"http://merchantslist.cu.cc",
	"^^^^^^^^^^^^^^^^^^^^^^^^^^",
	"Maraming salamat po ^^"
	);
my $repeat = "Press 0 to repeat the menu";
my $err = "Select only 1-5 and 0";
my $openRoom = 0;
my $childThread = 0;

Plugins::register("MerchChat", "Merchant List Introduction", \&on_unload, \&on_reload);
my $console = Commands::register(["ads", "Post Web Advertisement", \&ads]);
my $hooks = Plugins::addHooks(
		['packet/private_message', \&onPM, undef],
		['disconnected', \&dcUpdate, undef],
		['AI_post', \&main, undef]
	);
sub on_unload {
	Plugins::delHooks($hooks);
	Commands::unregister($console);
}

sub on_reload {	
	&on_unload;
}

sub main {
	if(AI::isIdle && $openRoom == 0){
		ads();
		changeTitle();
	}
}

sub changeTitle {
	if($childThread == 1){
		return;
	}
	$childThread = 1;

	my $pid = fork;
	return if $pid;	

	while (1) {
		sleep(60);
		if($openRoom == 1){
			modify_Room();
		}else{
			$childThread = 0;
			last;
		}
	}	
}

sub dcUpdate {
	if($net->getState() == Network::IN_GAME) {
		return;
	}

	message "Ads Disconnected... \n";
	$openRoom = 0;
}

sub ads {
	if (!$net || $net->getState() != Network::IN_GAME) {
		error TF("You must be logged in the game to use this command '%s'\n", shift);
		return;
	}

	if($openRoom == 1) {
		close_Room();
		return;
	}

	create_Room();
}

sub onPM {
	my (undef,$args) = @_;
	my $msg = $args->{privMsg};
	my $pmUser = $args->{privMsgUser};

	if(length($msg)>1){
		Commands::run("e heh");

		message "Displaying MENU\n";
		foreach(@menu){
			botReply($pmUser,$_);
		}		
	}else{
		if($msg =~ /[0-9]/){

				if($msg eq 0) { doMenu($pmUser); return 1; }
				if($msg eq 1) { ans($pmUser,@ans1); return 1; }
				if($msg eq 2) { ans($pmUser,@ans2); return 1; }
				if($msg eq 3) { ans($pmUser,@ans3); return 1; }
				if($msg eq 4) { ans($pmUser,@ans4); return 1; }
				if($msg eq 5) { ans($pmUser,@ans5); thx(); return 1; }

				botReply($pmUser,$err);

			}else{
				doMenu($pmUser);
			}
	}
}

sub thx {
	#We should thank them ^^
	my @rep = (
		"e no1",
		"e thx"
		);
	my $cnt = @rep;
	my $idx = int(rand($cnt-1));
	my $emo = $rep[$idx];

	Commands::run($emo);
}

sub ans {
	my($pmUser, @msg) = @_;

	foreach(@msg){
		botReply($pmUser ,$_);
	}
	botReply($pmUser, $repeat);
}

sub doMenu {
	my($pmUser) = @_;
	foreach(@menu){
		botReply($pmUser,$_);
	}
}

sub create_Room {
	my $cnt = @title;
	my $idx = int(rand($cnt-1));
	my $chatTitle = $title[$idx];

	message "Title: " . $chatTitle . "\n";
	$openRoom = 1;
	Commands::run("chat create \"" . $chatTitle . "\" 2 0 es ");
}

sub modify_Room {
	if($openRoom == 0) {
		return;
	}
	
	my $cnt = @title;
	my $idx = int(rand($cnt-1));
	my $chatTitle = $title[$idx];
	message "Changed: " . $chatTitle . "\n";

	Commands::run("chat modify \"" . $chatTitle  . "\" 2 0 es");
}

sub close_Room {	
	Commands::run("chat leave");
	$openRoom = 0;
}

# botReply(<user>,<msg>)
sub botReply {
	my($pmUser,$msg) = @_;
	Commands::run('pm "' . $pmUser . '" ' . $msg);
}

1;

#Version 1.0.3
# - Added AutoModify after 60 sec